<template>
  <div class="system-permission-edit addButton">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>系统管理</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/system/permission/list' }">权限列表</el-breadcrumb-item>
      <el-breadcrumb-item>权限修改</el-breadcrumb-item>
    </el-breadcrumb>
    <el-form size="small" label-width="150px" style="margin-top: 30px;" :model="options" :rules="rules" ref="form">
      <el-form-item label="权限关键字:" prop="permission">
        <el-input v-model="options.permission" placeholder="权限关键字" size="small"></el-input>
      </el-form-item>
      <el-form-item label="菜单名称:" prop="menuName">
        <el-input v-model="options.menuName" placeholder="菜单名称" size="small" maxlength="20"></el-input>
      </el-form-item>
      <el-form-item label="序号:" prop="serialNumber">
        <el-input v-model="options.serialNumber" placeholder="请填写菜单序号" size="small" type="number"></el-input>
      </el-form-item>
      <el-form-item label="路由地址:" prop="route">
        <el-input v-model="options.route" placeholder="路由名称" size="small"></el-input>
      </el-form-item>
      <el-form-item label="启用菜单" prop="menuOpend">
        <el-switch
          v-model="options.menuOpend"
          :active-value="1"
          :inactive-value="0"
          active-color="#ffb87e"
          inactive-color="#e1e1e1"
        ></el-switch>
      </el-form-item>
      <el-form-item label="图标:" prop="icon">
        <el-input v-model="options.icon" placeholder="图标名称" size="small" maxlength="50"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button size="small" style="background:#fb9654;color:#fff" @click="edit">修改</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
export default {
  mounted() {
    let data = sessionStorage.getItem("permissionEdit");
    if (data) {
      data = JSON.parse(data);
      this.options.id = data.id;
      this.options.permission = data.permission;
      this.options.menuName = data.menuName;
      this.options.route = data.route;
      this.options.parent = data.parentId;
      this.options.serialNumber = data.serialNumber;
      this.options.grade = data.grade;
      this.options.menuOpend = data.menuOpend;
    }
  },
  data() {
    const empty = (rule, value, callback) => {
      if (value.replace(/(^\s+)|(\s+$)/g, "") !== "") {
        callback();
      } else {
        callback(new Error("输入内容不得为空"));
      }
    };
    const serialNumber = (rule, value, callback) => {
      if (value !== "") {
        if (value > 20) {
          callback(new Error("数字不得大于20"));
        } else {
          callback();
        }
      } else {
        callback(new Error("序号不得为空"));
      }
    };
    return {
      options: {
        id:"", // 自身id
        permission: "", //权限关键字
        menuName: "", //菜单名称
        route: "", //路由地址
        icon: "", //图标地址
        parent: "", //父级id  1级菜单父级为0
        serialNumber: 0, //序号
        grade: 1, //权限等级 1/2/3 一级/二级/三级
        menuOpend: 1 //是否开启菜单 1/0 开启/关闭
      },
      rules: {
        menuName: [
          { required: true, validator: empty }
        ],
        serialNumber: [
          { required: true, validator: serialNumber }
        ]
      }
    };
  },

  methods: {
    edit() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          let option = {
            id:this.options.id, // 自身id
            permission: this.options.permission, //权限关键字
            menuName: this.options.menuName, //菜单名称
            route: this.options.route, //路由地址
            icon: this.options.icon, //图标地址
            parent: this.options.parent, //父级id  1级菜单父级为0
            serialNumber: this.options.serialNumber, //序号
            grade: this.options.grade, //权限等级 1/2/3 一级/二级/三级
            menuOpend: this.options.menuOpend //是否开启菜单 1/0 开启/关闭
          };
          this.$api.system.menuUpdate(option)
            .then((res) => {
              if (res.code === 200) {
                console.log(res);
                this.$message.success("修改成功");
              }
            });
        } else {
          return false;
        }
      });
    }
  }
};
</script>
<style scoped>

.system-permission-edit {
  padding: 30px;
}

.system-permission-edit .el-select {
  width: 230px;
}

.system-permission-edit .el-input {
  width: 230px;
}

.system-permission-edit .upload {
  display: inline-block;
}

.system-permission-edit .avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.system-permission-edit .avatar-uploader-icon {
  border: 1px dashed #949494;
}
</style>
